<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Flow;
use App\Models\Resume;
use App\Models\Step;
use App\Models\User;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Validator;
use App\libs\src\wxjsapi;

class FlowController extends Controller {
	private $company_id;
	private $user;
	private $role_flag;
	public function __construct() {
		if (\Auth::guard('api')->check()) {
			$user = \Auth::guard('api')->user();
			$data = User::find($user->id);
			if (!$this->getDevice()) {
				//app
				$data->company_id = $data->appcompany_id;
			}
			$this->company_id = $data->company_id;
			$this->user = $data;
			$company = \App\Models\Company::find($this->user->company_id);
			$this->role_flag = $company->role_flag;
		}
	}
	public function all(Request $request) {
		$data = Flow::where(['status' => 0, 'company_id' => $this->company_id])->select('id', 'name', 'is_use')->get();
		return $this->respond('get_ok', $data);
	}
	public function getAll(Request $request) {
		$data = Flow::where(['status' => 0, 'company_id' => $this->company_id, 'is_use' => 1])->select('id', 'name')->get();
		return $this->respond('get_ok', $data);
	}
	//新增保存
	public function add(Request $request) {
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		$roleType = $request->input('roleType');
		if ($roleType == 1) {
			if (!empty($staff) && $staff->company_id == $user->company_id) {
				if (!$user->can('flow_add') && !$user->hasRole('管理员')) {
					return $this->respond('no_auth');
				}
			} else {
				if (!$user->can('flow_add')) {
					return $this->respond('no_auth');
				}
			}
			return $this->respond('op_ok');
		}
		if (!empty($staff) && $staff->company_id == $user->company_id) {
			if (!$user->can('flow_add') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('flow_add')) {
				return $this->respond('no_auth');
			}
		}
		$company_id = $this->company_id;
		$validator = Validator::make($request->all(), Flow::$rules, Flow::$message);
		if ($validator->fails()) {
			return response()->json(['code' => 500, 'msg' => $validator->errors()->first()]);
		}
		$m = new Flow();
		$name = $request->input('name');
		$data = $m::where(['name' => $name, 'company_id' => $company_id, 'status' => 0])->first();
		if (!empty($data)) {
			return response()->json(['code' => 500, 'msg' => '流程名称不能重复']);
		}
		$result = DB::table('flows')->insertGetId(['name' => $name, 'company_id' => $company_id, 'created_at' => time(), 'updated_at' => time()]);
		if ($result) {
			return $this->respond('add_ok', Flow::find($result));
		}
		return $this->respond('add_error');
	}
	//更新保存
	public function put(Request $request, $id) {
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		if (!empty($staff) && $staff->company_id == $user->company_id) {
			if (!$user->can('flow_edit') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('flow_edit')) {
				return $this->respond('no_auth');
			}
		}
		$check_status = Resume::where(['company_id' => $this->company_id, 'flow_id' => $id, 'status' => 0])->first();
		if (!empty($check_status)) {
			$get_status = DB::select('select id from resumes where step_id>=1 and company_id=? and ask_status!=2 and (check_status=0||check_status=3) and status=0 limit 1', [$this->company_id]);
			if (!empty($get_status)) {
				return response()->json(['code' => 500, 'msg' => '流程使用中,不能进行修改操作！']);
			}
		}
		if (empty($id)) {
			return response()->json(['code' => 500, 'msg' => '流程id不能为空']);
		}
		$company_id = $this->company_id;
		$validator = Validator::make($request->all(), Flow::$rules, Flow::$message);
		if ($validator->fails()) {
			return response()->json(['code' => 500, 'msg' => $validator->errors()->first()]);
		}
		$m = new Flow();
		$model = $m::find($id);
		if (empty($model)) {
			return $this->respond('get_error');
		}
		$name = $request->input('name');
		$data = $m::where(['name' => $name, 'company_id' => $company_id, 'status' => 0])->where('id', '!=', $id)->first();
		if (!empty($data)) {
			return response()->json(['code' => 500, 'msg' => '流程名称不能重复']);
		}
		$model->name = $name;
		$result = $model->save();
		if ($result) {
			return $this->respond('update_ok');
		}
		return $this->respond('update_error');
	}
	//修改状态、开启、关闭
	public function status(Request $request, $id, $type) {
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		if (!empty($staff) && $staff->company_id == $user->company_id) {
			if (!$user->can('flow_use') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('flow_use')) {
				return $this->respond('no_auth');
			}
		}
		$check_status = Resume::where(['company_id' => $this->company_id, 'flow_id' => $id, 'status' => 0])->first();
		if (!empty($check_status)) {
			$get_status = DB::select('select id from resumes where step_id>=1 and company_id=? and ask_status!=2 and (check_status=0||check_status=3) and status=0 limit 1', [$this->company_id]);
			if (!empty($get_status)) {
				return response()->json(['code' => 500, 'msg' => '流程使用中,不能进行关闭操作！']);
			}
		}
		if (empty($id)) {
			return response()->json(['code' => 500, 'msg' => '流程id不能为空']);
		}
		$m = new Flow();
		$model = $m::find($id);
		if (empty($model)) {
			return $this->respond('get_error');
		}
		if (!in_array($type, [1, 2])) {
			return response()->json(['code' => '500', 'msg' => '类型参数格式不正确或无效']);
		}
		$model->is_use = $type;
		$result = $model->save();
		if ($result) {
			return $this->respond('op_ok');
		}
		return $this->respond('op_error');
	}
	//删除流程
	public function remove(Request $request, $id) {
		$user = $this->user;
		$user->guard_name = $this->role_flag;
		$staff = \App\Models\Staff::where(['status' => 0, 'mobile' => $user->mobile, 'role_status' => 1])->first();
		if (!empty($staff) && $staff->company_id == $user->company_id) {
			if (!$user->can('flow_remove') && !$user->hasRole('管理员')) {
				return $this->respond('no_auth');
			}
		} else {
			if (!$user->can('flow_remove')) {
				return $this->respond('no_auth');
			}
		}
		$check_status = Resume::where(['company_id' => $this->company_id, 'flow_id' => $id, 'status' => 0])->first();
		if (!empty($check_status)) {
			$get_status = DB::select('select id from resumes where step_id>=1 and company_id=? and ask_status!=2 and (check_status=0||check_status=3) and status=0 limit 1', [$this->company_id]);
			if (!empty($get_status)) {
				return response()->json(['code' => 500, 'msg' => '流程使用中,不能进行删除操作！']);
			}
		}
		if (empty($id)) {
			return response()->json(['code' => 500, 'msg' => '流程id不能为空']);
		}
		$m = new Flow();
		$model = $m::find($id);
		if (empty($model)) {
			return $this->respond('get_error');
		}
		$check_flow = Step::where(['status' => 0, 'flow_id' => $id])->first();
		if (!empty($check_flow)) {
			return response()->json(['code' => 500, 'msg' => '请先删除流程下的步骤']);
		}
		$model->status = 1;
		$result = $model->save();
		if ($result) {
			return $this->respond('delete_ok');
		}
		return $this->respond('delete_error');
	}
	public function step(Request $request, $id) {
/*		$user = \Auth::guard('api')->user();
		$user = \App\Models\User::find($user->id);
		if (!$this->getDevice()) {
			//app
			$user->company_id = $user->appcompany_id;
		}
		$time = time();
		//初始化部门
		$company_id = $user->company_id;
		$inti_position_id = \App\Models\Position::firstOrCreate(['user_id' => $user->id, 'company_id' => $company_id, 'name' => '示例', 'dept_name' => '示例'], Config::get('info.position_init_data')
		);
		$init_resume_id = \App\Models\Resume::insert(['position_id' => $inti_position_id], ['user_id' => $user->id, 'owner_id' => $user->id, 'company_id' => $company_id, 'name' => '示例', 'sex' => '男', 'mobile' => '10123456789', 'area' => '北京,北京,朝阳区', 'job_date' => '1420070400', 'birthday' => '631152000', 'position' => 'CEO', 'edu' => '博士及以上', 'company_name' => 'XX有限公司']);
		$check_resume_status = \App\Models\Job::where(['status' => 0, 'resume' => $init_resume_id])->first();
		if (empty($check_resume_status)) {

			\App\Models\Job::insert(['position_id'=>$init_resume_id,'type'=>'职能/高级管理,高级管理职位,CEO/总裁/总经理','name'=>'CEO/总裁/总经理','company_name'=>'XX有限公司','company_industry'=>'XX有限公司','content'=>'1.对公司的一切重大经营运作事项进行决策，包括对财务、经营方向、业务范围的增减等； 　　
2.参与董事会的决策，执行董事会的决议； 　　
3.主持公司的日常业务活动； 　　
4.对外签订合同或处理业务； 　　
5.任免公司的高层管理人员； 　　
6.定期向董事会报告业务情况，提交年度报告。 　　
7.树立、巩固或变更企业文化，团队建设等等','start_date'=>'1420041600','end_state'=>'1530403200']);
		}
		dd($re->id);*/
		if (empty($id)) {
			return response()->json(['code' => 500, 'msg' => '流程id不能为空']);
		}
		$data = Step::where(['status' => 0, 'flow_id' => $id])->select('id', 'name', 'role_id')->get();
		return $this->respond('get_ok', $data);
	}
}
